/**
 * @file Cyclic Necklace
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @brief 给定一个字符串，求往后追加几个，能构成循环
 *
 * KMP 求出 pattern 的最小循环节，
 * 看给定 pattern 的长度差几个字符能构成这个循环节的倍数
 * @version 1.0
 * @date 2022-05-08
 *
 * @copyright Copyright (c) 2022
 *
 **/

#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
char pattern[N];
#define next _next
int next[N];
int main() {
  // HDU 防寄大法
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> pattern + 1;
    int patternLen = strlen(pattern + 1);
    for (int iPos = 2, iMatchFront = 0; iPos <= patternLen; ++iPos) {
      if (iMatchFront > 0 && pattern[iPos] != pattern[iMatchFront + 1])
        iMatchFront = next[iMatchFront];
      if (pattern[iPos] == pattern[iMatchFront + 1]) iMatchFront++;
      next[iPos] = iMatchFront;
    }

    int cycleLength = patternLen - next[patternLen];
    int ans = 0;
    if (next[patternLen] == 0) {
      // 字符串里没有循环，克隆一条项链拼在后边
      ans = patternLen;  // 与原串同长
    } else if (patternLen % cycleLength == 0) {
      // 整除
      ans = 0;  // 完美，不需要增加
    } else {
      // 非整除
      ans = cycleLength - patternLen % cycleLength;
    }
    cout << ans << endl;
  }
}
